package com.baozhun.mall.common.base

import android.os.Bundle
import android.view.View
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.baozhun.mall.common.R
import com.baozhun.mall.common.databinding.FragmentBaseListBinding
import com.baozhun.mall.common.util.Constants
import com.blankj.utilcode.util.SizeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.listener.OnLoadMoreListener
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.fondesa.recyclerviewdivider.dividerBuilder
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel

/**
 *   列表基类
 *
 * @author xuefeng.zhu
 * @version 2.10.1
 * @since 2018/12/18/018
 */
abstract class BaseListFragment<VM : BaseViewModel, DB : FragmentBaseListBinding, ITEM_DB : ViewDataBinding, T : Any> :
    BaseFragment<VM, DB>(),
    OnRefreshListener, OnLoadMoreListener, OnItemChildClickListener, OnItemClickListener {

    /**
     * 基类列表
     */
    protected var mBaseListAdapter: BaseQuickAdapter<T, BaseDataBindingHolder<ITEM_DB>>? = null

    /**
     * 基类LayoutManager
     */
    private var mLayoutManager: RecyclerView.LayoutManager? = null

    /**
     * 是否为刷新，否则为上拉加载
     */
    private var mIsRefresh = false


    protected abstract fun createAdapter(): BaseQuickAdapter<T, BaseDataBindingHolder<ITEM_DB>>?
    abstract fun loadData(isRefresh: Boolean)
    protected open fun createLayoutManager(): RecyclerView.LayoutManager? = LinearLayoutManager(activity)
    protected open fun getEmptyView(): Int = R.layout.layout_empty

    override fun initView(savedInstanceState: Bundle?) {
        //初始化adapter
        mBaseListAdapter = createAdapter()
        mBaseListAdapter?.setOnItemChildClickListener(this)
        mBaseListAdapter?.setOnItemClickListener(this)
        mBaseListAdapter?.loadMoreModule?.setOnLoadMoreListener(this)
        mBaseListAdapter?.loadMoreModule?.isAutoLoadMore = true
        mBaseListAdapter?.setEmptyView(getEmptyView())
        //初始化recycleView
        mLayoutManager = createLayoutManager()
        mDataBinding?.baseRecycleView?.layoutManager = mLayoutManager
        mDataBinding?.baseRefreshRv?.setOnRefreshListener(this)
        mDataBinding?.baseRefreshRv?.setEnableLoadMore(false)
        if (getDividerSize() != 0)
            activity?.dividerBuilder()?.asSpace()?.size(SizeUtils.dp2px(getDividerSize().toFloat()))?.build()
                ?.addTo(mDataBinding.baseRecycleView)
        mDataBinding?.baseRecycleView?.adapter = mBaseListAdapter
    }

    override fun initData() {
        loadData(mIsRefresh)
    }

    /**
     * 刷新回调
     */
    override fun onRefresh(refreshLayout: RefreshLayout) {
        mIsRefresh = true
        loadData(mIsRefresh)
    }

    /**
     * 上拉加载更多
     */
    override fun onLoadMore() {
        mIsRefresh = false
        loadData(mIsRefresh)
    }

    /**
     * 获取分割线高度，单位dp
     */
    open fun getDividerSize(): Int {
        return 0
    }

    /**
     * 设置数据
     *
     * @param objects 加载的数据
     */
    protected fun setListData(objects: List<T>) {
        if (mIsRefresh) {
            onRefreshSuccess(objects)
        } else {
            onLoadMoreSuccess(objects)
        }
    }

    /**
     * 刷新成功
     *
     * @param objects 刷新加载的数据
     */
    private fun onRefreshSuccess(objects: List<T>?) {
        mDataBinding?.baseRefreshRv?.finishRefresh()
        mBaseListAdapter?.setList(objects)
    }

    /**
     * 分页成功
     *
     * @param objects 分页加载的数据
     */
    private fun onLoadMoreSuccess(objects: List<T>) {
        mIsRefresh = false
        mBaseListAdapter?.addData(objects)
        if (objects.size < Constants.Common.PAGE_SIZE) {
            mBaseListAdapter?.loadMoreModule?.loadMoreEnd(false)
        } else {
            mBaseListAdapter?.loadMoreModule?.loadMoreComplete()
        }
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {

    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
    }

}